/****************************************************************************
 * Copyright (c) 2017 ~ 2020.1 liangxie
 *
 * http://qframework.io
 * https://github.com/liangxiegame/QFramework
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ****************************************************************************/

using System;
using UnityEngine;
using UnityEngine.UI;

namespace QFramework
{
    [MonoSingletonPath("UIRoot")]
    public class UIRoot : MonoBehaviour, ISingleton
    {
        private Camera _uiCamera;
        private Canvas _canvas;
        private CanvasScaler _canvasScaler;
        private GraphicRaycaster _graphicRaycaster;

        private RectTransform _bg;
        private RectTransform _common;
        private RectTransform _popUI;
        private RectTransform _canvasPanel;

        public static UIRoot Instance { get; private set; }


        public Camera Camera => _uiCamera;


        public void Init(Camera uiCamera, Canvas canvas)
        {
            _uiCamera = uiCamera;
            _canvas = canvas;
            _uiCamera.name = "UICamera";
            _canvas.name = "Canvas";


            _uiCamera.transform.SetParent(transform, false);
            _canvas.transform.SetParent(transform, false);
            _graphicRaycaster = canvas.GetComponent<GraphicRaycaster>();
            _canvasScaler = canvas.GetComponent<CanvasScaler>();

            _bg     = CreateUILayer("UI_BG", -1);
            _common = CreateUILayer("UI_Common", -2);
            _popUI  = CreateUILayer("UI_PopUI", -3);
            
            if (Instance != null) Destroy(Instance);
            
            Instance = this;
            
            ScreenSpaceCameraRenderMode();
            return;

            
            RectTransform CreateUILayer(string layerName, int priority)
            {
                var obj = new GameObject(layerName);
                obj.transform.SetParent(_canvas.transform, false);
                return obj.transform as RectTransform;
            }
        }


        private void Start()
        {
            // 载入
        }


        public void OpenPanel()
        {
            
        }
        

        public void SetResolution(int width, int height, float matchOnWidthOrHeight)
        {
            _canvasScaler.referenceResolution = new Vector2(width, height);
            _canvasScaler.matchWidthOrHeight = matchOnWidthOrHeight;
        }

        public Vector2 GetResolution()
        {
            return _canvasScaler.referenceResolution;
        }

        public float GetMatchOrWidthOrHeight()
        {
            return _canvasScaler.matchWidthOrHeight;
        }

        public void ScreenSpaceOverlayRenderMode()
        {
            _canvas.renderMode = UnityEngine.RenderMode.ScreenSpaceOverlay;
            _uiCamera.gameObject.SetActive(false);
        }

        public void ScreenSpaceCameraRenderMode()
        {
            _canvas.renderMode = RenderMode.ScreenSpaceCamera;
            _uiCamera.gameObject.SetActive(true);
            _canvas.worldCamera = _uiCamera;
        }


        public void SetLevelOfPanel(UILevel level, IPanel panel)
        {
            switch (level)
            {
                case UILevel.Bg:
                    panel.Transform.SetParent(_bg);
                    break;
                case UILevel.Common:
                    panel.Transform.SetParent(_common);
                    break;
                case UILevel.PopUI:
                    panel.Transform.SetParent(_popUI);
                    break;
            }
        }

        public void OnSingletonInit() { }
    }
}